#include "qlineeditex.h"
#include <QDebug>

#include "protocol.h"
#include "protocol_07645.h"

QLineEditEx::QLineEditEx(QWidget *parent, enum DidType did_type_tmp)
    :QLineEdit(parent)//子类必须显式调用父类有参构造，否则父类的有参构造无法被调用
{
    QObject::installEventFilter(this);//安装事件过滤器

    did_type = did_type_tmp;
    pMainWin = this;

    while(1)//向上遍历，找到主窗体句柄
    {
        if( NULL == pMainWin->parentWidget() )//只有主窗体的父窗体为NULL
        {
            break;
        }
        pMainWin = pMainWin->parentWidget();
    }

    label_tip = new QLabel(pMainWin);//把label_tip设置为主窗体的子控件

    QString styleSheet_str =
            "QLabel\
            {\
                background-color: rgb(211, 230, 255);\
                border-style:solid;/*实线才有宽度/颜色*/\
                border-width:1px;\
                border-radius:5px;\
                border-color:rgb(163, 193, 229);\
                padding: 5px 5px 5px 5px;/*上右下左*/\
            }";//rgb(211, 230, 255)  (163, 193, 229)

    label_tip->setStyleSheet(styleSheet_str);
    label_tip->setMaximumWidth(500);
    label_tip->hide();

    label_bubble_arrow = new QLabel(pMainWin);
    label_bubble_arrow->setStyleSheet("QLabel{border-image: url(:/image/bubble.png);}");
    label_bubble_arrow->hide();
    //label_bubble_arrow.setGeometry();

}
void QLineEditEx::enterEvent(QEvent *event)
{
    //qDebug()<<"鼠标进入";
    event->accept();
    showDidAnalysis(true);
}
bool QLineEditEx::eventFilter(QObject *obj, QEvent *event)
{
    //需在构造函数中执行QObject::installEventFilter(this);//安装过滤器
    //本函数才能生效
    // qDebug()<<"lineEdit事件过滤"<<obj->objectName()<<event->type();
    if( obj == this )//事件来源:主窗体
    {
        //窗口停用，变为不活动的窗口
        if(QEvent::WindowActivate == event->type())//事件类型:所在主窗口被激活
        {
            showDidAnalysis(false);
            return true ;
        }
        else
        {
            return false ;
        }
    }
    return false ;
}

void QLineEditEx::leaveEvent(QEvent *event)
{
    //qDebug()<<"鼠标移出";
    event->accept();
    showDidAnalysis(false);
}
void QLineEditEx::showDidAnalysis(bool flag)
{
    if(true != flag)
    {
        label_tip->hide();
        label_bubble_arrow->hide();
        return;
    }

    QString did_str, DidAnalyStr,analysisType;
    did_str = this->text().toUpper();


    if(DidType_7E == this->did_type)
    {
        get_7E_did_analysis(did_str, DidAnalyStr,analysisType);
        //qDebug()<<"7E DID ANALY";
    }
    else
    {
        get_645_did_analysis(did_str, DidAnalyStr);
        //qDebug()<<"645 DID ANALY";
    }


    label_tip->setText(DidAnalyStr);

    QPoint posInScreen = this->mapToGlobal(QPoint(0,0));
    QPoint posInMainwin = pMainWin->mapFromGlobal(posInScreen);//获取did的输入框在主窗体中的位置

    label_tip->setGeometry(posInMainwin.x(), posInMainwin.y()+23,0,0);//把tip显示在did输入框的下面
    label_tip->adjustSize();//自适应文字的宽和高
    //label_tip->setMinimumHeight(25);
    label_tip->setMinimumWidth(this->width());
    label_tip->show();

    label_bubble_arrow->setGeometry(posInMainwin.x()+this->width()/3, posInMainwin.y()+23 -7,5,7);
    label_bubble_arrow->show();

}
